package com.admin.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.admin.bean.AdminUserBean;
import com.admin.bean.GameDataBean;
import com.admin.service.AdminUserService;
import com.admin.service.GameDataService;
import com.admin.util.DateUtil;

@Controller
@RequestMapping("/dateImport")
public class DateImportAction {

	@Autowired private GameDataService gameDataService;
	@Autowired private AdminUserService adminUserService;
	
	
	@RequestMapping("/upload")
	public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path = request.getSession().getServletContext().getRealPath("upload");
		
		System.out.println(path);
		String fileName = file.getOriginalFilename();
		System.out.println("fileName:"+fileName);
		File targetFile = new File(path, fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
		
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;
		try {
			fs = new POIFSFileSystem(new FileInputStream(targetFile));
			wb = new HSSFWorkbook(fs);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//wb.getNumberOfSheets();
		HSSFSheet sheet = wb.getSheetAt(0); // 获得第一个表单
		
		
		//遍历行
		for (int rowNumOfSheet = 1; rowNumOfSheet <= sheet.getLastRowNum(); rowNumOfSheet++) { 
			if (null != sheet.getRow(rowNumOfSheet)) { 
				HSSFRow aRow = sheet.getRow(rowNumOfSheet); 
				//System.out.println(aRow.getLastCellNum());
				//遍历列
				
				GameDataBean bean = new GameDataBean();
				
				HSSFCell channel = aRow.getCell((short)0);
				System.out.println(channel.getNumericCellValue());
				int cpid = (int)channel.getNumericCellValue();
				
				AdminUserBean userBean = adminUserService.findById(cpid);
				
				bean.setCpid(cpid);
				HSSFCell data1 = aRow.getCell((short)1);
				//System.out.println(data1.getNumericCellValue());
				int d1 = (int)data1.getNumericCellValue();
				bean.setData1(d1);
				HSSFCell data2 = aRow.getCell((short)2);
				//System.out.println(data2.getNumericCellValue());
				int d2 = (int)data2.getNumericCellValue();
				bean.setData2(d2);
				HSSFCell data3 = aRow.getCell((short)3);
				//System.out.println(data3.getNumericCellValue());
				int d3 = (int)data3.getNumericCellValue();
				bean.setData3(d3);
				HSSFCell data4 = aRow.getCell((short)4);
				//System.out.println(data4.getNumericCellValue());
				int d4 = (int)data4.getNumericCellValue();
				bean.setData4(d4);
				HSSFCell data7 = aRow.getCell((short)7);
				//System.out.println(data7.getDateCellValue());
				bean.setDt(DateUtil.formatDate(data7.getDateCellValue()));
				
				HSSFCell data8 = aRow.getCell((short)8);
				int spid = (int)data8.getNumericCellValue();
				bean.setSpid(spid);
				
				int cpd1 = d1*(100-userBean.getCppercent())/100;
				int cpd2 = d2*(100-userBean.getCppercent())/100;
				int cpd3 = d3*(100-userBean.getCppercent())/100;
				int cpd4 = ((d4*(100-userBean.getCppercent())/100)/2)*2;
				
				
				bean.setCpdata1(cpd1);
				bean.setCpdata2(cpd2);
				bean.setCpdata3(cpd3);
				bean.setCpdata4(cpd4);
				
				gameDataService.addGameDataBean(bean);
			}
		}
        
        //model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
        response.setContentType("text/html");
		response.getWriter().print("1");
		return null;
	}
	
	
	
	@RequestMapping("/videoUpload")
	public String videoUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path = request.getSession().getServletContext().getRealPath("upload");
		
		System.out.println(path);
		String fileName = file.getOriginalFilename();
		System.out.println("fileName:"+fileName);
		File targetFile = new File(path, fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
		
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;
		try {
			fs = new POIFSFileSystem(new FileInputStream(targetFile));
			wb = new HSSFWorkbook(fs);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//wb.getNumberOfSheets();
		HSSFSheet sheet = wb.getSheetAt(0); // 获得第一个表单
		
		
		//遍历行
		for (int rowNumOfSheet = 1; rowNumOfSheet <= sheet.getLastRowNum(); rowNumOfSheet++) { 
			if (null != sheet.getRow(rowNumOfSheet)) { 
				HSSFRow aRow = sheet.getRow(rowNumOfSheet); 
				//System.out.println(aRow.getLastCellNum());
				//遍历列
				
				GameDataBean bean = new GameDataBean();
				
				HSSFCell channel = aRow.getCell((short)0);
				
				System.out.println(channel.getNumericCellValue());
				int cpid = (int)channel.getNumericCellValue();
				AdminUserBean userBean = adminUserService.findById(cpid);
				bean.setCpid(cpid);
				HSSFCell data1 = aRow.getCell((short)1);
				//System.out.println(data1.getNumericCellValue());
				int d1 = (int)data1.getNumericCellValue();
				bean.setData1(d1);
				HSSFCell data2 = aRow.getCell((short)2);
				//System.out.println(data2.getNumericCellValue());
				int d2 = (int)data2.getNumericCellValue();
				bean.setData2(d2);
				HSSFCell data3 = aRow.getCell((short)3);
				//System.out.println(data3.getNumericCellValue());
				int d3 = (int)data3.getNumericCellValue();
				bean.setData3(d3);
				HSSFCell data4 = aRow.getCell((short)4);
				//System.out.println(data4.getNumericCellValue());
				int d4 = (int)data4.getNumericCellValue();
				bean.setData4(d4);
				HSSFCell data7 = aRow.getCell((short)7);
				//System.out.println(data7.getDateCellValue());
				bean.setDt(DateUtil.formatDate(data7.getDateCellValue()));
				
				HSSFCell data8 = aRow.getCell((short)8);
				int spid = (int)data8.getNumericCellValue();
				bean.setSpid(spid);
				
				
				int cpd1 = d1*(100-userBean.getCppercent())/100;
				int cpd2 = d2*(100-userBean.getCppercent())/100;
				int cpd3 = d3*(100-userBean.getCppercent())/100;
				int cpd4 = ((d4*(100-userBean.getCppercent())/100)/2)*2;
				
				bean.setCpdata1(cpd1);
				bean.setCpdata2(cpd2);
				bean.setCpdata3(cpd3);
				bean.setCpdata4(cpd4);
				
				gameDataService.addVideoDataBean(bean);
			}
		}
        
        //model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
        response.setContentType("text/html");
		response.getWriter().print("1");
		return null;
	}
}
